package com.atguigu.service.impl;

import com.atguigu.client.UcenterClient;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.domain.StatisticsDaily;
import com.atguigu.service.StatisticsDailyService;
import com.atguigu.mapper.StatisticsDailyMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【statistics_daily(网站统计日数据)】的数据库操作Service实现
* @createDate 2024-07-26 15:44:35
*/
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily>
    implements StatisticsDailyService{

    @Autowired
    private UcenterClient ucenterClient;


    @Override
    public void registerCount(String day) {
        R countRegister = ucenterClient.countRegister(day);
        Integer count = (Integer) countRegister.getData().get("countRegister");

        StatisticsDaily statisticsDaily = new StatisticsDaily();

        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setRegisterNum(count);


        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        List<String> data_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily statisticsDaily = staList.get(i);
            data_calculatedList.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",data_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}




